// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import GameEvent from "../../Commons/GameEvent";
import { CharacterType } from "../../Enums/GameEnum";
import { UINames } from "../../Enums/UIEnum";
import { randomRange } from "../../GlobalFunction";
import SaveManager from "../../Managers/SaveManager";
import UIManager from "../../Managers/UIManager";
import CharacterManager, { CharacterEvent } from "../../Models/Characters/CharacterManager";
import EquipManager from "../../Models/Equips/EquipManager";
import GateManager from "../../Models/Gates/GateManager";
import PetManager from "../../Models/Pets/PetManager";
import TaskManager from "../../Models/Tasks/TaskManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class CurrencyComp extends cc.Component {

    @property(cc.Label)
    goldTxt: cc.Label = null;
    @property(cc.Label)
    diamondTxt: cc.Label = null;
    @property([cc.Button])
    testButtonArr: cc.Button[] = [];
    @property(cc.Label)
    nameTxt: cc.Label = null;
    @property(cc.Sprite)
    headIcon: cc.Sprite = null;
    @property(cc.Node)
    setUI: cc.Node = null;
    @property(cc.EditBox)
    gateEditBox: cc.EditBox = null;

    protected onLoad(): void {
        this.setUI.active = false;
    }


    protected onEnable(): void {
        this.drawCurrency();
        // if (CC_BUILD) {
        //     for (const iterator of this.testButtonArr) {
        //         iterator.node.active = false;
        //     }
        // } else {
        //     for (const iterator of this.testButtonArr) {
        //         iterator.node.active = true;
        //     }
        // }
        // if (cc.sys.localStorage.getItem("accountName") != null) {
        //     this.nameTxt.string = cc.sys.localStorage.getItem("accountName");
        // } else {
        //     this.nameTxt.string = "？？？";
        // }
        this.nameTxt.string = SaveManager.single.userdata.playerName;
        this.onChangHead();

        GameEvent.eventNode.on(GameEvent.CURRENCYUPDATA_EVENT, this.drawCurrency, this);
        GameEvent.eventNode.on(GameEvent.CHANGEHEADID_EVET, this.onChangHead, this);
        GameEvent.eventNode.on(GameEvent.RENAME_EVENT, this.onRename, this);
        SaveManager.single.getGameNickname();

    }
    protected onDisable(): void {
        GameEvent.eventNode.off(GameEvent.CURRENCYUPDATA_EVENT, this.drawCurrency, this);
        GameEvent.eventNode.off(GameEvent.CHANGEHEADID_EVET, this.onChangHead, this);
        GameEvent.eventNode.off(GameEvent.RENAME_EVENT, this.onRename, this);

    }
    onRename() {
        this.nameTxt.string = SaveManager.single.userdata.playerName;
    }

    onChangHead() {
        var userdata = SaveManager.single.userdata;
        cc.resources.load("icons/" + userdata.playerHeadId, cc.SpriteFrame, (error: Error, spriteFrame: cc.SpriteFrame) => {
            this.headIcon.spriteFrame = spriteFrame;
        });
    }

    drawCurrency() {
        var userdata = SaveManager.single.userdata;
        this.goldTxt.string = "" + userdata.gold;
        this.diamondTxt.string = "" + userdata.diamond;
    }

    addGoldClick() {
        if (CC_DEBUG) {
            var userdata = SaveManager.single.userdata;
            userdata.gold += 100000000;
            TaskManager.single.requestUpdataTaskPregress(103, 10000);
            TaskManager.single.requestUpdataTaskPregress(109, 10000);
            SaveManager.single.saveData();
            GameEvent.eventNode.emit(GameEvent.CURRENCYUPDATA_EVENT);
            SaveManager.single.gameDataReport();
        }

    }

    addEquipClick() {
        if (CC_DEBUG) {
            var arr = EquipManager.single.getEquipTemplateArr();
            // var index = randomRange(0, arr.length - 1);
            for (const iterator of arr) {
                EquipManager.single.requestAddEquip(iterator.id);
            }
            var equipBagList = SaveManager.single.userdata.equipBagList;
            for (const iterator of equipBagList) {
                iterator.level = 5;
            }
            SaveManager.single.saveData();
            SaveManager.single.gameDataReport();
        }
    }
    addEquipAndLevel10Click() {
        if (CC_DEBUG) {
            var arr = EquipManager.single.getEquipTemplateArr();
            // var index = randomRange(0, arr.length - 1);
            for (const iterator of arr) {
                EquipManager.single.requestAddEquip(iterator.id);
            }
            var equipBagList = SaveManager.single.userdata.equipBagList;
            for (const iterator of equipBagList) {
                iterator.level = 10;
            }
            SaveManager.single.saveData();
            SaveManager.single.gameDataReport();
        }
    }
    addPetClick() {
        if (CC_DEBUG) {
            var arr = PetManager.single.getPetTemplateArr();
            for (const iterator of arr) {
                PetManager.single.requestGetPet(iterator.id);
            }
            var petBagList = SaveManager.single.userdata.petBagList;
            for (const iterator of petBagList) {
                iterator.level = 5;
            }

            SaveManager.single.saveData();
            SaveManager.single.gameDataReport();
        }
    }

    addPetAnd10Click() {
        var arr = PetManager.single.getPetTemplateArr();
        for (const iterator of arr) {
            PetManager.single.requestGetPet(iterator.id);
        }
        var petBagList = SaveManager.single.userdata.petBagList;
        for (const iterator of petBagList) {
            iterator.level = 10;
        }

        SaveManager.single.saveData();
        SaveManager.single.gameDataReport();
    }

    setGateClick() {
        let gateNum: number = Number(this.gateEditBox.string);

        SaveManager.single.userdata.gateEntity.currentGateId = gateNum;
        SaveManager.single.saveData();
        if (SaveManager.single.userdata.gatePassList.indexOf(SaveManager.single.userdata.gateEntity.currentGateId) == -1) {
            SaveManager.single.userdata.gatePassList.push(SaveManager.single.userdata.gateEntity.currentGateId);

        }
        PetManager.single.requseUnlockWear();

        SaveManager.single.gameDataReport();
    }
    testUpgradeSkillHalf() {
        var skillList = SaveManager.single.userdata.skillList;

        for (const iterator of skillList) {
            iterator.level = Math.ceil(iterator.skillTp.maxLevel / 2);
        }
        SaveManager.single.saveData();
        UIManager.single.showTips("升级成功。");
        SaveManager.single.gameDataReport();
    }
    testUpgradeSkillMax() {
        var skillList = SaveManager.single.userdata.skillList;

        for (const iterator of skillList) {
            iterator.level = iterator.skillTp.maxLevel;
        }
        SaveManager.single.saveData();
        UIManager.single.showTips("升级成功。");
        SaveManager.single.gameDataReport();
    }

    testUnlockAllAndLevel100() {
        var tps = CharacterManager.single.getAllCharacters(CharacterType.player);
        for (const iterator of tps) {
            CharacterManager.single.requestUnlockHero(iterator.id);
        }

        var heroList = SaveManager.single.userdata.heroList;
        for (const iterator of heroList) {
            iterator.lv = 100;
        }

        SaveManager.single.saveData();
        GameEvent.eventNode.emit(CharacterEvent.UPGRADE_EVENT);
        SaveManager.single.gameDataReport();
    }

    testUnlockAllAnddLevel200() {
        var tps = CharacterManager.single.getAllCharacters(CharacterType.player);
        for (const iterator of tps) {
            CharacterManager.single.requestUnlockHero(iterator.id);
        }

        var heroList = SaveManager.single.userdata.heroList;
        for (const iterator of heroList) {
            iterator.lv = 200;
        }

        SaveManager.single.saveData();
        GameEvent.eventNode.emit(CharacterEvent.UPGRADE_EVENT);
        SaveManager.single.gameDataReport();
    }



    playerHeadClick() {
        this.setUI.active = !this.setUI.active;


    }
}
